﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Newtonsoft.Json;
using System.Windows.Media.Media3D;
using TotalApplication.Core;
using TotalApplication.Mine_Video;
using TotalApplication.viewmodels.Video.Controls;
using TotalApplication.views.Video;

namespace TotalApplication.viewmodels.Video
{
    public partial class TagsVM : BaseViewModel
    {
        [ObservableProperty]
        private VideoTags? _videoTags;

        [ObservableProperty]
        private TagLIstVM _tags_Artist = new TagLIstVM();

        [ObservableProperty]
        private TagLIstVM _tags_Charactor = new TagLIstVM();

        [ObservableProperty]
        private TagLIstVM _tags_Special = new TagLIstVM();

        [ObservableProperty]
        private TagLIstVM _tags_Category = new TagLIstVM();


        [ObservableProperty]
        private TagTreeItemVM _testData = new TagTreeItemVM()
        {
            Name = "Test",
            Depth = 0,
            Childs = new System.Collections.ObjectModel.ObservableCollection<TagTreeItemVM>()
            {
                new TagTreeItemVM()
                {
                    Name = "测试1",
                    Childs = new System.Collections.ObjectModel.ObservableCollection<TagTreeItemVM>()
                    {
                        new TagTreeItemVM()
                        {
                            Name = "测试1-1",
                            Childs = new System.Collections.ObjectModel.ObservableCollection<TagTreeItemVM>()
                            {
                                new TagTreeItemVM()
                                {
                                    Name = "测试1-1-1"
                                },
                                new TagTreeItemVM()
                                {
                                    Name = "测试1-1-2"
                                }
                            }
                        },
                        new TagTreeItemVM()
                        {
                            Name = "测试1-2"
                        }
                    }
                },
                new TagTreeItemVM()
                {
                    Name = "测试2",
                    Depth = 2,
                    Childs = new System.Collections.ObjectModel.ObservableCollection<TagTreeItemVM>()
                    {
                        new TagTreeItemVM()
                        {
                            Name = "测试2-1"
                        },
                        new TagTreeItemVM()
                        {
                            Name = "测试2-2"
                        }
                    }
                },
                new TagTreeItemVM()
                {
                    Name = "测试3",
                    Depth = 3
                }
            }
        };

        private int _selectIndex = 0;

        /// <summary>
        /// 当前选中的标签页
        /// </summary>
        public int SelectIndex
        {
            get => _selectIndex;
            set
            {
                if(SetProperty(ref _selectIndex, value))
                switch (value)
                {
                    case 0:
                        Tags = Tags_Artist;
                        break;
                    case 1:
                        Tags = Tags_Charactor;
                        break;
                    case 2:
                        Tags = Tags_Special;
                        break;
                    case 3:
                        Tags = Tags_Category;
                        break;
                }
            }
        }

        /// <summary>
        /// 当前操作的标签列表
        /// </summary>
        private TagLIstVM _tags = new TagLIstVM();

        public TagLIstVM Tags
        {
            get { return _tags; }
            set { if(SetProperty(ref _tags, value))
                {
                    //_tags.State = 1;
                    //_tags.Edit();
                } 
            }
        }



        #region Private Properties


        #endregion

        #region Constructor

        /// <summary>
        /// Default constructor
        /// </summary>
        public TagsVM(TagItemOB tagItemOB)
        {
            Tags_Artist = tagItemOB._tags_Artist;
            Tags_Charactor = tagItemOB._tags_Charactor;
            Tags_Category = tagItemOB._tags_Category;
            Tags_Special = tagItemOB._tags_Special;

            switch (SelectIndex)
            {
                case 0:
                    Tags = Tags_Artist;
                    break;
                case 1:
                    Tags = Tags_Charactor;
                    break;
                case 2:
                    Tags = Tags_Special;
                    break;
                case 3:
                    Tags = Tags_Category;
                    break;
            }
        }

        [JsonConstructor]
        public TagsVM()
        {

        }

        #endregion

        [RelayCommand]
        public void AddItem()
        {
            updateSize();
            Tags.Add();
        }

        [RelayCommand]
        public void Edit()
        {
            updateSize();
            Tags.Edit();
        }

        public void ClosePopup()
        {
            switch (SelectIndex)
            {
                case 0:
                    Tags_Artist.ClosePopup();
                    break;
                case 1:
                    Tags_Charactor.ClosePopup();
                    break;
                case 2:
                    Tags_Special.ClosePopup();
                    break;
                case 3:
                    Tags_Category.ClosePopup();
                    break;
            }
        }

        //public void Delete(TagItemVM item)
        //{
        //    updateSize();
        //    switch (SelectIndex)
        //    {
        //        case 0:
        //            Tags_Artist.Tags.Remove(item);
        //            break;
        //        case 1:
        //            Tags_Charactor.Tags.Remove(item);
        //            break;
        //        case 2:
        //            Tags_Special.Tags.Remove(item);
        //            break;
        //        case 3:
        //            Tags_Category.Tags.Remove(item);
        //            break;
        //    }
        //}

        //public void Top(TagItemVM item)
        //{
        //    updateSize();
        //    switch (SelectIndex)
        //    {
        //        case 0:
        //            Tags_Artist.Top(item);
        //            break;
        //        case 1:
        //            Tags_Charactor.Top(item);
        //            break;
        //        case 2:
        //            Tags_Special.Top(item);
        //            break;
        //        case 3:
        //            Tags_Category.Top(item);
        //            break;
        //    }
        //}

        //public void Expand(TagItemVM item)
        //{
        //    switch (SelectIndex)
        //    {
        //        case 0:
        //            Tags_Artist.Expand(item);
        //            break;
        //        case 1:
        //            Tags_Charactor.Expand(item);
        //            break;
        //        case 2:
        //            Tags_Special.Expand(item);
        //            break;
        //        case 3:
        //            Tags_Category.Expand(item);
        //            break;
        //    }
        //}

        private void updateSize()
        {
            if (Tags.Parent != null || VideoTags == null)
                return;
            Tags.Width = VideoTags.Width - 20;
            Tags.Height = VideoTags.Height - 50;
        }
    }
}
